<template>
  <div class="relative w-full pb40">
    <SearchCom
      :searchList="searchList"
      @search="handleSearch"
      @reset="handleReset"
    />
    <el-table
      :data="tableData"
      style="width: 100%"
      class="cover-table mt12"
      border
      v-loading="loading"
    >
      <el-table-column :label="$t('sellerAnnualFee.table.序号')" width="80">
        <template #default="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="tenantId" :label="$t('sellerAnnualFee.table.商户ID')" />
      <el-table-column :prop="'tenantName' + locale" :label="$t('sellerAnnualFee.table.店铺名称')" />

      <el-table-column prop="tenantCode" :label="$t('sellerAnnualFee.table.店主账号')" />
      <el-table-column prop="tenantSettleContact" :label="$t('sellerAnnualFee.table.联系人')" />
      <el-table-column prop="tenantSettlePhone" :label="$t('sellerAnnualFee.table.联系人电话')" />

      <el-table-column prop="ruleDuration" :label="$t('sellerAnnualFee.table.缴纳年限')">
        <template #default="scope">
          <span>{{ scope.row.ruleDuration }}{{ $t('datePick.年') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ruleFee" :label="$t('sellerAnnualFee.table.金额')">
        <template #default="scope">
          <span>{{ scope.row.ruleFee }}{{ $t('currency.元') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" :label="$t('sellerAnnualFee.table.凭证上传时间')">
        <template #default="scope">
          <span>{{
            moment(scope.row.createdTime).format("YYYY-MM-DD HH:mm:ss")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="payImage" :label="$t('sellerAnnualFee.table.凭证')">
        <template #default="scope">
          <div class="flex_aic gap-12 flex-wrap">
            <el-image
              v-for="img in scope.row.payImage?.split(',') || []"
              style="width: 60px; height: 60px"
              :src="img"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[img]"
              show-progress
              fit="cover"
              :preview-teleported="true"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="checkStatus" :label="$t('sellerAnnualFee.table.审核状态')">
        <template #default="scope">
          <el-tag :type="statusMap[scope.row.checkStatus]?.color">
            {{ statusMap[scope.row.checkStatus]?.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="action" :label="$t('sellerAnnualFee.table.操作')" fixed="right" width="120">
        <template #default="scope">
          <el-button
            v-if="scope.row.checkStatus === 0"
            type="text"
            @click="handleAudit(scope.row)"
            v-my-point="'tenantRenew:check'"
          >
            {{ $t('sellerAnnualFee.table.审核') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-end mt24 absolute bottom-0 right-0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <AuditDia
      v-model="showAuditDia"
      :data="auditData"
      @success="getDataList"
    ></AuditDia>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import Api from "@/config/api/api";
import { useI18n } from "vue-i18n";
import moment from "moment";
import SearchCom from "@/components/SearchCom.vue";
import AuditDia from "./auditDia.vue";
const { locale, t } = useI18n();

const searchList = [
  {
    label: t("sellerAnnualFee.table.商户ID"),
    key: "tenantId",
    type: "input",
  },
  {
    label: t("sellerAnnualFee.table.店铺名称"),
    key: "tenantName",
    type: "input",
  },
  {
    label: t("sellerAnnualFee.table.店主账号"),
    key: "tenantCode",
    type: "input",
  },
  {
    label: t("sellerAnnualFee.table.联系人"),
    key: "tenantSettleContact",
    type: "input",
  },
  {
    label: t("sellerAnnualFee.table.联系人电话"),
    key: "tenantSettlePhone",
    type: "input",
  },
];

const props = defineProps({
  type: {
    type: String,
    default: "1",
  },
});
const showAuditDia = ref(false);
const auditData = ref({});
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tableData = ref([]);
const loading = ref(false)
const statusMap = {
  0: {
    label: t("sellerAnnualFee.table.待审核"),
    color: "warning",
  },
  1: {
    label: t("sellerAnnualFee.table.审核通过"),
    color: "success",
  },
  "-1": {
    label: t("sellerAnnualFee.table.审核失败"),
    color: "danger",
  },
};
const handleSizeChange = (size) => {
  pageSize.value = size;
  currentPage.value = 1;
  getDataList();
};
const handleCurrentChange = (page) => {
  currentPage.value = page;
  getDataList();
};
const handleSearch = (params) => {
  currentPage.value = 1;
  getDataList(params);
};
const handleReset = () => {
  currentPage.value = 1;
  getDataList();
};
const getDataList = (params) => {
  loading.value = true
  Api.tenantRenewList({
    page: currentPage.value - 1,
    size: pageSize.value,
    ...params,
    ["tenantName" + locale.value]: params?.tenantName,
  }).then((res) => {
    tableData.value = res.data.content || [];
    total.value = Number(res.data.totalElements || 0);
  }).finally(()=>{
    loading.value = false;
  });
};

onMounted(() => {
  getDataList();
});

const handleAudit = (row) => {
  showAuditDia.value = true;
  auditData.value = row;
};
</script>

<style lang="scss" scoped>
.addBtn {
  height: 28px;
  margin-left: 10px;
  padding: 0 7px;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
}
.split {
  width: 1px;
  height: 12px;
  background: #ddd;
  margin: 0 5px;
}
</style>
